package pATT.core.visual;

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.io.File;
import java.util.Enumeration;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import pATT.bNEdit.base.RedBayes;
import pATT.core.InstanceManager;
import pATT.core.Chooser.ImageFilter;
import pATT.logger.Message;


public class OtherFileDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JScrollPane jScrollPane = null;
	private JTextArea jTextArea = null;
	private JButton jButton = null;
//	public String EXTENSION = "";
	
	public OtherFileDialog(String name, String content) throws HeadlessException {
		super();
		initialize(name, content);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize(String title, String content) {
		this.setTitle(title);
		this.setSize(452, 334);
		this.setModal(true);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		getJTextArea().setText(content);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension freim = this.getSize();
		if (pantalla.width < freim.width){
			freim.width = pantalla.width;
		}
		if (pantalla.height < freim.height) {
			freim.height = pantalla.height;
		}
		this.setLocation((pantalla.width-freim.width)/2,(pantalla.height-freim.height)/2);
		this.show();
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getJButton(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new java.awt.Rectangle(1,0,443,258));
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setEditable(false);
		}
		return jTextArea;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jButton.setBackground(java.awt.SystemColor.control);
			jButton.setBounds(new java.awt.Rectangle(158,263,121,26));
			jButton.setText("OK");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				@SuppressWarnings("unchecked")
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(getTitle().endsWith("."+ImageFilter.PERSONAL_SEARCHER_EXTENSION)){
						
						StringTokenizer tokenizer = new StringTokenizer(getJTextArea().getText(),"\n");
						UserProfilingUI.eINSTANCE.updateRelationInfo("Personal Searcher", -1, tokenizer.countTokens());
						//se le pasa un Enumeration con lo que serian los atributos
						UserProfilingUI.eINSTANCE.updateAttributesInfo(tokenizer);
						
					}else if(getTitle().endsWith("."+ImageFilter.SAVER_EXTENSION)){
						
						File netBayes = new File(Message.PATH_NET_BAYES_DEFAULT);

						File file = InstanceManager.eINSTANCE.getFileLoaded();
						RedBayes net = new RedBayes("");

						net.loadFromXml03File(netBayes.getAbsolutePath());
						String data = net.load(file.getAbsolutePath());

						net.updateNet(data);

//						Vector nodos = net.ListaNodos();
						Enumeration variables = net.ListaVariables().elements();

						
						//pasarle el nombre d ela red de bayes y la cantidad de atributos.
						UserProfilingUI.eINSTANCE.updateRelationInfo(net.getNombre(), -1, net.ListaVariables().size());
						//se le pasa una lista con lo que serian los atributos
						UserProfilingUI.eINSTANCE.updateAttributesInfo(variables);
						
					}

					dispose();
				}
			});

		}
		return jButton;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
